package com.inforeach.util.string;

import org.junit.Assert;
import org.junit.Test;

public class StringSequenceGenerator_utest
{

    @Test
    public void test_e()
    {
        Assert.assertEquals("", get(new StringSequenceGenerator(StringSequences.DIGIT10_STR), 0));
        Assert.assertEquals("0", get(new StringSequenceGenerator(StringSequences.DIGIT10_STR), 1));
        Assert.assertEquals("00", get(new StringSequenceGenerator(StringSequences.DIGIT10_STR), 11));
        Assert.assertEquals("000", get(new StringSequenceGenerator(StringSequences.DIGIT10_STR), 111));
        Assert.assertEquals("0000", get(new StringSequenceGenerator(StringSequences.DIGIT10_STR), 1111));
    }

    @Test
    public void test_e_AZ()
    {
        Assert.assertEquals("", get(new StringSequenceGenerator(StringSequences.LATIN26_UPPER_STR), 0));
        Assert.assertEquals("A", get(new StringSequenceGenerator(StringSequences.LATIN26_UPPER_STR), 1));
        Assert.assertEquals("Z", get(new StringSequenceGenerator(StringSequences.LATIN26_UPPER_STR), 26));
        Assert.assertEquals("AA", get(new StringSequenceGenerator(StringSequences.LATIN26_UPPER_STR), 1 + 26));
        Assert.assertEquals("ZZ", get(new StringSequenceGenerator(StringSequences.LATIN26_UPPER_STR), 26 + 26 * 26));
        Assert.assertEquals("AAA", get(new StringSequenceGenerator(StringSequences.LATIN26_UPPER_STR), 1 + 26 + 26 * 26));
    }

    @Test
    public void test_100()
    {
        Assert.assertEquals("200", get(new StringSequenceGenerator(StringSequences.DIGIT10_STR, "100"), 100));
        Assert.assertEquals("0000", get(new StringSequenceGenerator(StringSequences.DIGIT10_STR, "100"), 900));
    }

    @Test(expected=IllegalArgumentException.class)
    public void testEmptyAlphabet()
    {
        new StringSequenceGenerator("");
    }

    @Test(expected=IllegalArgumentException.class)
    public void testDuplicates()
    {
        new StringSequenceGenerator("01231");
    }

    @Test(expected=IllegalArgumentException.class)
    public void testUnknownCharacter()
    {
        new StringSequenceGenerator("0123", "12A");
    }

    @Test(expected=UnsupportedOperationException.class)
    public void testRemove()
    {
        new StringSequenceGenerator("01").iterator().remove();
    }

    String get(StringSequenceGenerator generator, int index)
    {
        for (String value : generator)
        {
            if (index-- == 0)
            {
                return value;
            }
        }
        throw new Error("unrechable");
    }
}
